#pragma once

#include <map>

#include <SFML/Graphics/Drawable.hpp>

#include "block.h"

class Player;

class Level : public sf::Drawable
{
public:
    static const int Width = 20;
    static const int Height = 15;

    static Level *loadFile(const std::string &filename);
    static bool saveFile(const Level &map, const std::string &filename);

    Level(void);
    virtual ~Level(void);

    inline Block *blockAt(int x, int y) const
    {
        assert(x < Width);
        assert(y < Height);
        int pos = x + y*Width;
        std::map<int, Block *>::const_iterator block = m_blocks.find(pos); 

        if (block == m_blocks.end())
            return 0;

        return block->second;
    }

    inline Block *blockAt(const sf::Vector2i &pos) const { return blockAt(pos.x, pos.y); }

    void setBlock(int x, int y, Block::Type type)
    {
        assert(x < Width);
        assert(y < Height);
        int pos = x + y*Width;

        if (!m_blocks.count(pos))
            m_blocks[pos] = new Block;

        m_blocks[pos]->setType(type);
        m_blocks[pos]->SetPosition(float(x*Block::Width), float(y*Block::Height));
    }

    void checkCollisions(Player &player);

protected:
    std::map<int, Block *> m_blocks;

private:
    void Render(sf::RenderTarget &target) const;
};
